import os
import sys
import time

sys.path.append(os.path.dirname(__file__))


def randdatasetGeneratedProcess():
    fileNames = ["correlated10k.txt", "correlated20k.txt", "correlated50k.txt", "independent10k.txt",
                 "independent20k.txt",
                 "independent50k.txt", "anti-correlated10k.txt", "anti-correlated20k.txt", "anti-correlated50k.txt", ]
    for fileName in fileNames:
        dataInputArray = []
        with open('DataSetTxt/' + fileName, "r") as file:
            # Loop through each line in the file
            for line in file:
                # Split the line into an array of strings
                row = line.strip().split(',')
                # Append each row to the dataInputArray
                dataInputArray.append(row)
        currentMilliTime = int(round(time.time() * 1000))
        BNL(len(dataInputArray), len(dataInputArray[0]), dataInputArray)
        print(fileName + ':BNL Elapsed time: {} ms'.format(int(round(time.time() * 1000)) - currentMilliTime))
        currentMilliTime = int(round(time.time() * 1000))
        SFS(len(dataInputArray), len(dataInputArray[0]), dataInputArray)
        print(fileName + ':SFS Elapsed time: {} ms'.format(int(round(time.time() * 1000)) - currentMilliTime))


if __name__ == '__main__':
    from BNL import BNL
    from SFS import SFS
    from BBS import BBS

    currentMilliTime = int(round(time.time() * 1000))
    dataInputArray = []
    # Open the file containing the data (file: qws_sub.txt or qws_normal.txt)
    fileNameIndex = input(
        'Please enter the number of these file names:\n1. qws_sub.txt\n2. qws_normal.txt\n3. randdataset Generated\n')
    if fileNameIndex == '1':
        fileName = 'DataSetTxt/qws_sub.txt'
    elif fileNameIndex == '2':
        fileName = 'DataSetTxt/qws_normal.txt'
    elif fileNameIndex == '3':
        randdatasetGeneratedProcess()
        sys.exit()
    else:
        print('Invalid input!')
        sys.exit()
    if not os.path.exists(fileName):
        print('File not found!')
        sys.exit()
    with open(fileName, "r") as file:
        # Loop through each line in the file
        for line in file:
            # Split the line into an array of strings
            row = line.strip().split()
            # Append each row to the dataInputArray
            dataInputArray.append(row)
    functionChoice = input('Please enter your choice of these algorithms:\n1. BNL\n2. SFS\n3. BBS\n')
    # Call the BNL/SFS/BBS function with the parameters of the dataInputArray
    if functionChoice == '1':
        result = BNL(len(dataInputArray), len(dataInputArray[0]), dataInputArray)
    elif functionChoice == '2':
        result = SFS(len(dataInputArray), len(dataInputArray[0]), dataInputArray)
    elif functionChoice == '3':
        result = BBS(len(dataInputArray), len(dataInputArray[0]), dataInputArray)
    else:
        print('Invalid input!')
        sys.exit()
    # Print the result
    print('Output result:')
    print(result)
    print('Length of the result: {}'.format(len(result)))
    # Print the elapsed time
    elapsedTime = int(round(time.time() * 1000)) - currentMilliTime
    print('Elapsed time: {} ms'.format(elapsedTime))
    # Save the result to a file
    with open('result.txt', 'w') as file:
        file.write(str(result))
        file.flush()
        file.close()
        print('Result saved!')
        sys.exit()